package com.javajun.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.javajun.custom.Success;
import com.javajun.entity.Admin;
import com.javajun.entity.Student;
import com.javajun.entity.SysUser;
import com.javajun.entity.Teacher;
import com.javajun.service.AdminService;
import com.javajun.service.StudentServcie;
import com.javajun.service.SysUserService;
import com.javajun.service.TeacherService;

@Controller
@RequestMapping(value="/user")
public class UserController {

    @Autowired
    private SysUserService sysUserService;
    
    @Autowired
    private AdminService adminService;
    
    @Autowired
    private StudentServcie studentService;
    
    @Autowired
    private TeacherService teacherService;
    
    @RequestMapping(value="/manage")
    public String manageView(ModelMap map) {
        String username = sysUserService.getCurrentUser().getUsername();
        SysUser model = sysUserService.findByUsername(username);
        String role = model.getSysRole().get(0).getCode();
        map.put("model", model);
        map.put("role", role);
        return "user/manage";
    }
    
    @RequestMapping(value="/editPassword", method=RequestMethod.GET)
    public String editPasswordView(ModelMap map) {
        String username = sysUserService.getCurrentUser().getUsername();
        SysUser model = sysUserService.findByUsername(username);
        String role = model.getSysRole().get(0).getCode();
        map.put("model", model);
        map.put("role", role);
        return "user/editPassword";
    }
    
    @RequestMapping(value="/checkpwd", method=RequestMethod.POST)
    @ResponseBody
    public Object checkpwdView(String oldpassword, Integer id) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String password = sysUserService.findById(id).getPassword();
        Success success;
        boolean matches = encoder.matches(oldpassword, password);
        if(matches) {
            success = new Success(true, "原密码正确");
        } else {
            success = new Success(false, "原密码错误");
        }
        return success;
    }
    
    @RequestMapping(value="/editPassword", method=RequestMethod.POST)
    @ResponseBody
    public Object editPassword(Integer id, String oldpassword, String password, String role) {
        Success success;
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if(role.equals("ROLE_ADMIN")) {
            System.out.println("是管理员");
            Admin admin = adminService.findById(id);
            admin.setPassword(encoder.encode(password));;
            try {
                adminService.save(admin);
                success = new Success(true, "该管理员修改密码成功");
            } catch (Exception e) {
                success = new Success(false, "出错啦。这个邮箱已经注册了哦~~");
            }
        } else if(role.equals("ROLE_STUDENT")) {

            Student student = studentService.findById(id);
            student.setPassword(encoder.encode(password));
            try {
                studentService.save(student);
                success = new Success(true, "该学生修改密码成功");
            } catch (Exception e) {
                success = new Success(false, "出错啦。已经注册了哦~~");
            }
        } else if(role.equals("ROLE_TEACHER")) {
            Teacher teacher = teacherService.findById(id);
            teacher.setPassword(encoder.encode(password));
            try {
                teacherService.save(teacher);
                success = new Success(true, "修改密码成功");
            } catch (Exception e) {
                success = new Success(false, "出错啦~~");
            }
        } else {
            System.out.println("不知道什么用户");
            success = new Success(false, "未知身份的用户。。");
        }
        return success;
    }
    
    @RequestMapping(value="/save")
    @ResponseBody
    public Object saveDo(Integer id, String name, String email, String phone, String role) {
        Success success;
        if(role.equals("ROLE_ADMIN")) {
            Admin admin = adminService.findById(id);
            admin.setName(name);
            admin.setEmail(email);
            admin.setPhone(phone);
            try {
                adminService.save(admin);
                success = new Success(true, "个人资料修改成功");
            } catch (Exception e) {
                success = new Success(false, "出错啦。这个邮箱已经注册了哦~~");
            }
        } else if(role.equals("ROLE_STUDENT")) {

            Student student = studentService.findById(id);
            student.setName(name);
            student.setEmail(email);
            student.setPhone(phone);
            try {
                studentService.save(student);
                success = new Success(true, "个人资料修改成功");
            } catch (Exception e) {
                success = new Success(false, "出错啦。这个邮箱已经注册了哦~~");
            }
        } else if(role.equals("ROLE_TEACHER")) {
            Teacher teacher = teacherService.findById(id);
            teacher.setName(name);
            teacher.setEmail(email);
            teacher.setUsername(email);
            teacher.setPhone(phone);
            try {
                teacherService.save(teacher);
                success = new Success(true, "个人资料修改成功");
            } catch (Exception e) {
                success = new Success(false, "出错啦。这个邮箱已经注册了哦~~");
            }
        } else {
            System.out.println("不知道什么用户");
            success = new Success(false, "未知身份的用户。。");
        }
        return success;
    }
}
